package com.ruoyi.framework.web.task;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;

import java.util.List;
import java.util.concurrent.RecursiveAction;

/**
 * description:
 * auth fcb
 * date 2025-04-01 15:12
 **/
public class PasswordGenerateTask extends RecursiveAction {

    private List<SysUser> list;

    private int start;

    private int end;

    private int threshold = 100;

    public PasswordGenerateTask(List<SysUser> users, int start, int end) {
        this.list = users;
        this.start = start;
        this.end = end;
    }
    @Override
    protected void compute() {
        if (end - start <= threshold) { // 直接处理小任务
            for (int i = start; i < end; i++) {
                SysUser user = list.get(i);
                String userName = user.getUserName();
                String pwd = SecurityUtils.encryptPassword(userName.substring(userName.length() - 6));
                user.setPassword(pwd); // BCrypt加密
            }
        } else { // 拆分任务
            int middle = (start + end) / 2;
            invokeAll(
                    new PasswordGenerateTask(list, start, middle),
                    new PasswordGenerateTask(list, middle, end));
        }
    }


}
